package puppy.demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;

import util.ir.QueryIndex;
import util.nlp.HashDictionary;
import util.nlp.LanguageModel;
import util.nlp.Node;

public class Launcher {

	static String dmoz_tags = "/home/sergio/projects/data/delicious/experiments/dmoz_tags.txt";
	static String tags_normalization = "/home/sergio/Documents/delicious_stats/tags_dictionary.txt";
	static String tags_clusters = "/home/sergio/Documents/delicious_stats/tags_clusters.txt";;
	static HashDictionary dmozTags = new HashDictionary(dmoz_tags);

	static HashDictionary tagsNormalized = new HashDictionary(
			tags_normalization);

	static HashDictionary tagsClusters = new HashDictionary(tags_clusters);

	public static HashSet<String> tagsMerge(ArrayList<Node> list,
			Hashtable<String, Float> hash) {

		HashSet<String> set = new HashSet<String>();

		Enumeration<String> keys = hash.keys();

		while (keys.hasMoreElements()) {

			String key = keys.nextElement();

			LinkedList<String> l = dmozTags.getEntries(key);
			// System.out.println(key+ "\t" + l);
			if (l != null)
				set.addAll(l);
		}

		for (int i = 0; i < list.size(); i++) {

			set.add(list.get(i).word);
		}

		return set;

	}

	public static HashSet<String> normalized(HashSet<String> set) {

		HashSet<String> set2 = new HashSet<String>();

		Iterator<String> iter = set.iterator();
		while (iter.hasNext()) {

			String n = iter.next();
			LinkedList<String> list = tagsNormalized.getEntries(n);

			// System.out.println(list);
			if (list != null)
				set2.addAll(list);
		}

		return set2;

	}

	
	public static HashSet<String> getSuggestions(String query) throws CorruptIndexException, ParseException, IOException{
		
		/*String fields[] = new String[2];
		fields[0] = "summary";
		fields[1] = "title";
		
		String index = "/home/sergio/projects/indexes/index_dmoz.txt";

		Hashtable<String, Float> hash = new Hashtable<String, Float>();
		QueryIndex.multiFieldSearch(query, fields, "url", index, hash, 2);

		ArrayList<Node> list = new ArrayList<Node>();*/
		
		String path = "/home/sergio/projects/data/delicious/experiments/dmoz_del_approx1.txt";
		 LanguageModel.generate_aspectModel(path);
		 

	//	HashSet<String> set = tagsMerge(list, hash);
	//	HashSet<String> set2 = normalized(set);

		return null;
	//	return set2;
		
		
	}
	public static void main(String args[]) throws CorruptIndexException,
			ParseException, IOException {

		String path = "/home/sergio/projects/data/delicious/experiments/dmoz_del_approx1.txt";

		 LanguageModel.generate_aspectModel(path);

		//String index = "/home/sergio/projects/indexes/index_dmoz.txt";

		String fields[] = new String[2];
		fields[0] = "summary";
		fields[1] = "title";

		Scanner in = new Scanner(System.in);

		// Reads a single line from the console
		// and stores into name variable

		String word = "";
		while (!word.equals("exit")) {

			System.out.print(">");
			word = in.nextLine();

			Hashtable<String, Float> hash = new Hashtable<String, Float>();
		//	QueryIndex.multiFieldSearch(word, fields, "url", index, hash, 2);

			ArrayList<Node> list = new ArrayList<Node>();

			HashSet<String> set = tagsMerge(list, hash);
			HashSet<String> set2 = normalized(set);

			Hashtable<String, HashSet<String>> clusters = getClusters(set2, 1);

			Enumeration<String> keys = clusters.keys();

			System.out.println("Facets found:");
			while (keys.hasMoreElements()) {

				String key = keys.nextElement();

				System.out.println(key);

				HashSet<String> set1 = clusters.get(key);

				Iterator<String> iter = set1.iterator();

				while (iter.hasNext()) {

					System.out.println("\t" + iter.next());

				}
			}

			System.out.println();
		}

		// Reads a integer from the console
		// and stores into age variable

		in.close();

	}

	private static HashSet<String> getClusters(HashSet<String> set2) {
		// TODO Auto-generated method stub

		HashSet<String> set = new HashSet<String>();

		Iterator<String> iter = set2.iterator();

		while (iter.hasNext()) {

			String key = iter.next();

			LinkedList<String> list = tagsClusters.getEntries(key);
			if (list != null)
				set.addAll(list);
		}

		return set;
	}

	
	public static Hashtable<String, HashSet<String>> getClusters(
			HashSet<String> set2, int q) {
		// TODO Auto-generated method stub

		Hashtable<String, HashSet<String>> hash = new Hashtable<String, HashSet<String>>();

		Iterator<String> iter = set2.iterator();

		while (iter.hasNext()) {
			String key = iter.next();
			LinkedList<String> list = tagsClusters.getEntries(key);

			for (int i = 0; list != null && i < list.size(); i++) {

				String l = list.get(i);

				String t[] = l.trim().split("-");

				if (t.length > 0) {
					t[0] = t[0].toLowerCase().replace(" ", "").trim();
					if (hash.containsKey(t[0])) {
						HashSet<String> temp = hash.get(t[0]);

						for (int j = 1; j < t.length; j++) {

							String tt = t[j].trim().toLowerCase().replaceAll(
									" ", "");

							temp.add(tt);

						}

					} else {

						HashSet<String> temp = new HashSet<String>();

						for (int j = 1; j < t.length; j++) {

							String tt = t[j].trim().toLowerCase().replaceAll(
									" ", "");

							temp.add(tt);

						}

						hash.put(t[0], temp);
					}
				}

			}

		}

		return hash;
	}

}
